import React from "react";
import { useState } from 'react'
import './App.css'

const App = () => {
    const [flag, setFlag] = useState('');
    const [data, setData] = useState({})

    const changeD = (f, e) => {
        if (f === 'u') {
            setData({ ...data, username: e.target.value })
        } else {
            setData({ ...data, passwd: e.target.value })
        }
    }


    const searchItem = () => {

        fetch('http://39.96.84.223:4567/verify/adminlogin', {
            //POST请求
            method: 'POST',
            headers: {
                'Accept': '*/*',
                'content-type': 'application/json'
            },
            body: JSON.stringify(data)
        })
            .then(res => res.json())
            .then(res => {
                console.log(res);
                //改状态
                setFlag(`<div><h2>登陆成功</h2><br/><p>学校是${res.userinfo.university}</p></div>`)

            })

            .catch((e) => {
                console.log(e.message);
                setFlag(`登陆失败`)
            })
    }

    return (
        <div className='box'>
            <input type='text' placeholder='请输入账号' onChange={(e) => changeD('u', e)}></input>
            <br />
            <input type='password' placeholder='请输入密码' onChange={(e) => changeD('p', e)}></input>
            <br />
            <button onClick={searchItem}>登录</button>
            <br />
            {flag === '登陆失败' ? <span>登陆失败</span> : <div dangerouslySetInnerHTML={{ __html: flag }}></div>
            }
        </div>

    )

}

export default App;